import { createStore } from 'vuex'
import { getStorage, hideLoading, showLoading } from '@/common/util'
import api from '@/common/api'
interface SelAddrData {
	id : string
}
const selAddr = {} as SelAddrData
const commonStore = createStore({
	//存放状态
	state: {
		"globalType": {},
		"reload": [],
		"selAddr": selAddr,
		"selLandObj":{}
	},
	mutations: {
		setSelLandObj(state, data) {
			state.selLandObj = data
		},
		setGlobalType(state, data) {
			state.globalType = data
		},
		addReload(state, pageName : string) {
			const idx = state.reload.indexOf(pageName)
			if (idx < 0) state.reload.push(pageName)
		},
		delReload(state, pageName : string) {
			const idx = state.reload.indexOf(pageName)
			if (idx >= 0) state.reload.splice(idx, 1)
		},
		setSelAddr(state, data) {
			state.selAddr = data
		},
	},
	actions: {
		async getDefType(e, type : string) {
			console.info(e, type)
			const old = commonStore.state.globalType
			if (old[type]) return

			showLoading()
			try {
				const res = await api.getDefType(type)

				old[type] = res.data
				this.commit('setGlobalType', old)
			} finally {
				hideLoading()
			}

		}
	}
})

export default commonStore